//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using DNAide.Core;

namespace DNAide.Web.UI.Controls
{

	/// <summary>
	/// Simple control that renders a message in a div with a CSS class related to the type of error message.
	/// Also works well for displaying a list of DNAide.Core.Validation errors. Renders these as an unordered list.
	/// </summary>
	public class ServerMessage : System.Web.UI.Control
	{
		private string _message;
		private MessageDisplayType _messageType;
		private List<ValidationError> _validationErrors;

		/// <summary>
		/// </summary>
		public enum MessageDisplayType
		{
			/// <summary></summary>
			Normal,
			/// <summary></summary>
			Warning,
			/// <summary></summary>
			Error
		}

		#region Properties

		/// <summary>
		/// </summary>
		public String Message
		{
			get { return _message; }
		}

		/// <summary>
		/// </summary>
		public MessageDisplayType MessageType
		{
			get { return _messageType; }
		}

		/// <summary>
		/// </summary>
		public List<ValidationError> ValidationErrors
		{
			get
			{
				if (_validationErrors == null)
					_validationErrors = new List<ValidationError>();
				return _validationErrors;
			}
		}

		/// <summary>
		/// </summary>
		public String NormalCssClass
		{
			get { return ((ViewState["NormalCssClass"] == null) ? "server-message" : (string)ViewState["NormalCssClass"]); }
			set { ViewState["NormalCssClass"] = value; }
		}

		/// <summary>
		/// </summary>
		public String ErrorCssClass
		{
			get { return ((ViewState["ErrorCssClass"] == null) ? "server-message-error" : (string)ViewState["ErrorCssClass"]); }
			set { ViewState["ErrorCssClass"] = value; }
		}

		/// <summary>
		/// </summary>
		public String WarningCssClass
		{
			get { return ((ViewState["WarningCssClass"] == null) ? "server-message-warning" : (string)ViewState["WarningCssClass"]); }
			set { ViewState["WarningCssClass"] = value; }
		}

		/// <summary>
		/// String format for each ValidationError, default "{0}: {1}" = FriendlyFieldName: ErrorMessage
		/// </summary>
		public String ValidationErrorStringFormat
		{
			get { return ((ViewState["WarningCssClass"] == null) ? "{0}: {1}" : (string)ViewState["WarningCssClass"]); }
			set { ViewState["WarningCssClass"] = value; }
		}

		#endregion

		/// <summary>
		/// </summary>
		public ServerMessage()
		{
			_message = string.Empty;
			_messageType = MessageDisplayType.Normal;
		}

		/// <summary>
		/// </summary>
		/// <param name="message"></param>
		public void SetMessage(String message)
		{
			SetMessage(message, MessageDisplayType.Normal);
		}

		/// <summary>
		/// </summary>
		/// <param name="message"></param>
		/// <param name="messageType"></param>
		public void SetMessage(String message, MessageDisplayType messageType)
		{
			_message = CastAide.AsStringInvariant(message);
			_messageType = messageType;
		}

		/// <summary>
		/// </summary>
		/// <param name="message"></param>
		/// <param name="messageType"></param>
		/// <param name="validationErrors"></param>
		public void SetMessage(String message, List<ValidationError> validationErrors, MessageDisplayType messageType)
		{
			_message = message;
			_validationErrors = validationErrors;
			_messageType = messageType;
		}

		/// <summary>
		/// </summary>
		/// <param name="writer"></param>
		protected override void Render(HtmlTextWriter writer)
		{
			if (_message.Length > 0)
			{
				writer.WriteBeginTag("div");
				writer.WriteAttribute("id", ClientID);
				switch (_messageType)
				{
					case MessageDisplayType.Error:
						writer.WriteAttribute("class", ErrorCssClass);
						break;
					case MessageDisplayType.Warning:
						writer.WriteAttribute("class", WarningCssClass);
						break;
					default:
						writer.WriteAttribute("class", NormalCssClass);
						break;
				}
				writer.Write(HtmlTextWriter.TagRightChar);
				writer.Write(_message);
				if (ValidationErrors.Count > 0)
				{
					writer.WriteFullBeginTag("ul");
					foreach (ValidationError err in ValidationErrors)
					{
						writer.WriteFullBeginTag("li");
						if (err.FriendlyFieldName.Length > 0)
						{
							writer.Write(String.Format(ValidationErrorStringFormat, err.FriendlyFieldName, err.ErrorMessage));
						}
						else
						{
							writer.Write(err.ErrorMessage);
						}
						writer.WriteEndTag("li");
					}
					writer.WriteEndTag("ul");
				}
				writer.WriteEndTag("div");
			}
		}
	}
}

